﻿using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SK3000RunningTestTool.SKServerSource
{
    public class ServerSource
    {
        public static List<SKServer> SKServerList = new List<SKServer>();

        public static bool Init()
        {
            bool bolResult = false;
            SKServerList = new List<SKServer>();
            XmlNodeList lstNode = CommonMethod.XmlUse.GetNodeListInfo(Parameter.ServerXMLDataFilePath, Parameter.ServerXMLParentNodeName, Parameter.ServerXMLNodeName);
            foreach (XmlNode node in lstNode)
            {
                SKServerList.Add(GetServerInfoByXmlNode(node));
            }
            return bolResult;
        }


        public static SKServer GetServerInfoByXmlNode(XmlNode node)
        {
            SKServer server = new SKServer();
            server.ServerID = Convert.ToInt32(node.Attributes["ServerID"].Value);
            server.ServerName= node.Attributes["ServerName"].Value;
            server.ServerAddress= node.Attributes["ServerAddress"].Value;
            server.ServerStatus = Enum_ServerStatus.Unknown;
            XmlNode MonistionService = node.SelectSingleNode("MonitionService");
            if (MonistionService != null)
            {
                server.monServer = (Service_MonitorService)GetService(MonistionService);
            }
            XmlNode Temp_nodeServiceList = node.SelectSingleNode("SKServiceList");
            XmlNodeList Temp_lstNode = Temp_nodeServiceList.SelectNodes("SKService");
            server.ServiceList = new List<ISKService>();
            foreach (XmlNode item in Temp_lstNode)
            {
                ISKService iservice = GetService(item);
                iservice.Server = server;
                server.ServiceList.Add(iservice);
            }
            return server;
        }

        public static ISKService GetService(XmlNode node)
        {
            ISKService result = new Service_TCPService();
            Enum_ServiceTestType typeId = (Enum_ServiceTestType)Convert.ToInt32(node.Attributes["TypeID"].Value);
            switch (typeId)
            {
                case Enum_ServiceTestType.TCPService:
                    result = new Service_TCPService();
                    break;
                case Enum_ServiceTestType.ATUDSerice:
                    result = new Service_ATDU();
                    break;
                case Enum_ServiceTestType.DeviceService:
                    result = new Service_Device();
                    break;
                case Enum_ServiceTestType.VDTUService:
                    result = new Service_VTDU();
                    break;
                case Enum_ServiceTestType.VideoRecordService:
                    result = new Service_VideoRecord();
                    break;
                case Enum_ServiceTestType.WebService:
                    result = new Service_WebService();
                    break;
                case Enum_ServiceTestType.WebSocket:
                    result = new Service_WebSocket();
                    break;
                case Enum_ServiceTestType.MonitorService:
                    result = new Service_MonitorService();
                    break;
            }
            result.ID = Convert.ToInt32(node.Attributes["ID"].Value);
            result.ServiceAddress = Convert.ToString(node.Attributes["ServiceAddress"].Value);
            result.ServicePort = Convert.ToUInt16(node.Attributes["ServicePort"].Value);
            result.ServiceName = node.Attributes["ServiceName"].Value;
            result.ServiceRemark = node.Attributes["ServiceRemark"].Value;
            return result;
        }


        public static string GetServerStatusString(Enum_ServerStatus value)
        {
            string strResult = "未知状态";
            switch (value)
            {
                case Enum_ServerStatus.Unknown:
                    strResult= "未知状态";
                    break;
                case Enum_ServerStatus.InService:
                    strResult = "运行中";
                    break;
                case Enum_ServerStatus.StandBy:
                    strResult = "待机中";
                    break;
                case Enum_ServerStatus.Abnormal:
                    strResult = "异常";
                    break;
            }
            return strResult;
            
        }

    }
}
